package com.wteam.dragon.system.apps.security.config;

import com.wteam.dragon.system.exception.BadRequestException;
import com.wteam.dragon.system.util.UserUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * el表达式
 * 用于权限审核
 * @Author 陈晓辉
 * @Date 2020/5/1 15:22
 */
@Service(value = "el")
public class ElPermissionConfig {
    public Boolean check(String ...permissions){
        if(UserUtil.getCurrentUser() == null){
            throw new BadRequestException(HttpStatus.UNAUTHORIZED , "认证失败");
        }
        // 获取当前用户的所有权限
        List<String> elPermissions = UserUtil.getCurrentUser().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
        // 判断当前8用户的所有权限是否包含接口上定义的权限
        return elPermissions.contains("admin") || Arrays.stream(permissions).anyMatch(elPermissions::contains);
    }
}
